package com.xjrsoft.core.tool.utils;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.xjrsoft.common.config.JPushConfig;

import java.util.List;

public class JPushUtil {


    public static boolean push(String message, List<String> registrationId,Integer type) {
        if (CollectionUtil.isNotEmpty(registrationId)) {
            JPushConfig jPushConfig = SpringUtil.getBean(JPushConfig.class);
            JPushClient jPushClient = jPushConfig.getJPushClient();
            PushPayload payload = PushPayload.newBuilder()
                    .setPlatform(Platform.all())
                    .setAudience(Audience.registrationId(registrationId))
                    .setNotification(Notification.newBuilder()
                            .setAlert(message)
                            .addPlatformNotification(AndroidNotification.newBuilder()
                                    .addExtra("type",type)
                                    .build())
                            .addPlatformNotification(IosNotification.newBuilder()
                            .addExtra("type",type)
                            .build())
                            .build()).setOptions(Options.newBuilder().setApnsProduction(true)
                            .build())
                    .build();
            try {
                PushResult pushResult = jPushClient.sendPush(payload);
                if (pushResult != null && pushResult.getResponseCode() == 200) {
                    return true;
                } else {
                    return false;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        }
        return false;
    }
}
